/**
 * @filename:SysConfigController Apr 5, 2023
 * @project Wonder Java前后台分离框架  1.1.0
 * Copyright(c) 2020 Wonder Co. Ltd. 
 * All right reserved. 
 */
package com.wonder.admin.controller;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Date;
import java.util.Map;

import com.wonder.admin.domain.SysConfig;
import com.wonder.admin.service.SysConfigService;
import com.wonder.admin.request.SysConfigRequest;
import com.wonder.utils.DBUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.wonder.common.common.AjaxMap;
import com.wonder.common.common.AjaxResult;
import com.wonder.common.common.CommonPage;
import com.wonder.common.utils.MessageUtils;
import com.wonder.common.utils.StringUtils;
import com.alibaba.fastjson.JSON;
import com.wonder.utils.DBUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
/**   
 * 
 * <p>说明： 基本配置API接口层</P>
 * @version: 1.1.0
 * @author: Wonder
 * @time    Apr 5, 2023
 *
 */
@Api(tags = "基本配置")
@RestController
@RequestMapping("/admin/Baseconfig")
public class SysConfigController {
	
	@Autowired
	private SysConfigService sysConfigService;
	
	@Autowired
	private DBUtils dbUtils;
	
	/**
	 * @explain 配置表单
	 * @param   entity
	 * @return  Boolean
	 * @author  Wonder
	 */
	@PostMapping("/index")
	@ApiOperation(value = "配置表单", notes = "作者：Wonder")
	public AjaxResult<SysConfig> index(@RequestBody Map<String, Object> entity){
		
		Map<String, Object> res=sysConfigService.getSysConfig();
		
		if(entity.containsKey("keyword")){
		    List<String> keywords=StringUtils.castList(entity.get("keyword"),String.class);
			entity.put("keyword", StringUtils.join(keywords, ','));
		}	
			
		if(entity.containsKey("transport_fee"))
			entity.put("transport_fee", JSON.toJSONString(entity.get("transport_fee")));
			
		entity.forEach((key,value)->{
			if(res.containsKey(key))
				sysConfigService.update(new UpdateWrapper<SysConfig>().eq("name", key).set("data", value));
			else {
				SysConfig sysConfig=new SysConfig();
				sysConfig.setData(value.toString());
				sysConfig.setName(key);
				
				sysConfigService.save(sysConfig);
			}
			
		});

		return AjaxResult.success();
		
	}

	/**
	 * @explain 获取sql语句定义的字段信息
	 * @return  Boolean
	 * @author  Wonder
	 */
	@PostMapping("/getFieldList")
	@ApiOperation(value = "获取sql语句定义的字段信息", notes = "作者：Wonder")
	public AjaxResult<Map<String,Object>> getFieldList(){
		Map<String,Object> smallTable=new HashMap<>();
		smallTable.put("default_agent_reserves",dbUtils.getDropDownList("select id,name from sn_agent_reserve"));
		return AjaxResult.success(smallTable);
	}

	/**
	 * @explain 获取基本表单信息
	 * @return  Boolean
	 * @author  LuZhongren
	 * @time    Apr 5, 2023
	 */
	@PostMapping("/getInfo")
	@ApiOperation(value = "获取基本配置信息", notes = "作者：LuZhongren")
	public AjaxResult<Map<String, Object>> getInfo(){
		Map<String, Object> res=sysConfigService.getSysConfig();
		if(res.containsKey("keyword"))
		   res.put("keyword", StringUtils.split(res.get("keyword").toString(), ','));
		if(res.containsKey("transport_fee"))
		   res.put("transport_fee", JSON.parseObject(res.get("transport_fee").toString(),
					new ArrayList<Map<String, Object>>().getClass()));

		Map<String,Object> smallTable=new HashMap<>();

		return AjaxResult.success(res);
	}


	/**
	 * @explain 获取基本表单信息
	 * @return  Boolean
	 * @author  LuZhongren
	 * @time    Jan 8, 2023
	 */
	@PostMapping("/getKey")
	@ApiOperation(value = "获取基本配置信息", notes = "作者：LuZhongren")
	public AjaxResult<Object> getKey(@RequestBody Map<String,Object> map){
		if(map==null)return AjaxResult.failed("system.parameterError");
		String key=map.get("key").toString();
		return AjaxResult.data(sysConfigService.getCacheKey(key));
	}

	
}